//
//  PTTaskListVC.m
//  PotentialTraining
//
//  Created by admin on 2018/2/1.
//  Copyright © 2018年 admin. All rights reserved.
//

#import "PTTaskListVC.h"
#import "trainCell.h"
#import "PTReadCheckDetailVC.h"
#import "PTReadCheckModel.h"

@interface PTTaskListVC ()<UICollectionViewDelegate,UICollectionViewDataSource>

@property (nonatomic, strong) NSMutableArray *dataArray;

@property (nonatomic, strong) UICollectionView *classCollectionView;

@end

@implementation PTTaskListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self setUpNav];
    
    [self layoutSubControls];
    
    [self loadData];
}

- (void) layoutSubControls {
    
    /**
     *
     *我要写一个NB的collectionView
     */
    // 这个是系统提供的布局类，可以布局一些比较规则的布局。
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    // 设置每个item的大小，
    flowLayout.itemSize = CGSizeMake((kScreenWidth-200- 120)/3, (kScreenWidth-200- 120)/3);//宽高
    // 设置列的最小间距，cell之间的距离
    flowLayout.minimumInteritemSpacing = 0;
    // 设置最小行间距
    flowLayout.minimumLineSpacing = 20;
    // 设置布局的内边距
    flowLayout.sectionInset = UIEdgeInsetsMake(20, 20, 20, 20);
    // 滚动方向
    flowLayout.scrollDirection = UICollectionViewScrollDirectionVertical;//纵向滑动
    self.classCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-64) collectionViewLayout:flowLayout];
    flowLayout.headerReferenceSize = CGSizeMake(0, 0);
    [self.classCollectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"UICollectionViewHeader"];
    self.classCollectionView.showsVerticalScrollIndicator = NO;
    self.classCollectionView.backgroundColor = [UIColor whiteColor];
    // 设置代理
    self.classCollectionView.delegate = self;
    self.classCollectionView.dataSource = self;
    [self.classCollectionView registerNib:[UINib nibWithNibName:@"trainCell" bundle:nil] forCellWithReuseIdentifier:@"trainCell"];
    [self.view addSubview:self.classCollectionView];
}


#pragma mark - UICollectionView Delegate
- (BOOL)collectionView:(UICollectionView *)collectionView canMoveItemAtIndexPath:(NSIndexPath *)indexPath NS_AVAILABLE_IOS(9_0){
    return NO;
}

// 返回分区数
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

// 每个分区多少个item
- (NSInteger )collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    if (self.dataArray.count) {
        
        return self.dataArray.count;
    } else {
        
        return 0;
    }
    
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    trainCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"trainCell" forIndexPath:indexPath];
    //    cell.backgroundColor = [UIColor redColor];
    //    NSArray *ary = self.dataAry[indexPath.section];
    //    cell.titleLabel.text = [NSString stringWithFormat:@"%@",ary[indexPath.item]];
    
    if (self.dataArray.count) {
        
        PTReadCheckModel *model = self.dataArray[indexPath.row];
        
        [cell configBookInfo:model];
    }
    
    return cell;
    
}

- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    PTReadCheckDetailVC *readCheckDetailVC = [PTReadCheckDetailVC new];
    
    PTReadCheckModel *model = self.dataArray[indexPath.row];
    
    readCheckDetailVC.content = model.content;
    readCheckDetailVC.bookId = model.book_id;
    [self.navigationController pushViewController:readCheckDetailVC animated:YES];
}

- (void) loadData {
    
    
    NSString *getUrl = [NSString stringWithFormat:@"%@%@", [[VersionManager shareManager] currentVersion], kTask];
    kDLog(@"获取任务页面接口是：%@", getUrl);
    
    [CHNetWorkSingleton requestAFWithURL:getUrl params:nil httpMethod:@"GET" isHUD:NO finishBlock:^(id result) {
        
        NSArray *bookArray = result[@"data"];
        NSString *code = result[@"code"];
        NSLog(@"获取任务页面返回的信息是：%@", result);
        if ([code isEqualToString:@"11"]) {
            self.dataArray = [NSMutableArray array];
            for (NSDictionary *bookDic in bookArray) {
                
                PTReadCheckModel *model = [PTReadCheckModel mj_objectWithKeyValues:bookDic];
                [self.dataArray addObject:model];
            }
        }
        
        [self.classCollectionView reloadData];
    } errorBlock:^(NSError *error) {
        
        kDLog(@"获取任务页失败，错误是：%@", error);
    }];
    
}

- (void) setUpNav
{
    
    self.title = @"任务列表";
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
